package com.example.cd5160866.tabs.camera;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.example.cd5160866.tabs.R;

public class CameraViewActivity extends AppCompatActivity {
    private CameraView cameraView;

    private boolean hasOpened = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_view_main);
        cameraView = (CameraView) findViewById(R.id.camera);
        findViewById(R.id.btn_open).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hasOpened = true;
                cameraView.openCamera();
                cameraView.startPreview();
            }
        });
        findViewById(R.id.btn_take).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cameraView.takePhoto();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (hasOpened) {
            cameraView.openCamera();
            cameraView.startPreview();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        cameraView.release();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cameraView.release();
    }
}
